<template>
  <div>
    <my-breadcrumb :items="['商家大全', '裙子管理']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增裙子</el-button>
      <el-button type="primary" @click="toExport" :loading="loading">导出裙子</el-button>
    </div>
    <el-table :data="skirts" border @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="上传时间"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="商家名称"
        prop="membername">
      </el-table-column>
      <el-table-column
        label="裙子名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="图片"
        width="60px"
        prop="img1">
        <template slot-scope="scope">
          <img :src="scope.row.img1" alt="" width="40">
        </template>
      </el-table-column>
      <el-table-column
        label="团号"
        width="70px"
        sortable="custom"
        prop="uniteid">
      </el-table-column>
      <el-table-column
        label="风格分类"
        prop="styleid">
        <template slot-scope="scope">
          {{ style[scope.row.styleid - 1] ? style[scope.row.styleid - 1].name : '' }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否过期">
        <template slot-scope="scope">
          {{ scope.row.status === '0' ? '否' : '已过期' }}
        </template>
      </el-table-column>
      <el-table-column
        label="服装分类"
        prop="clothingid">
        <template slot-scope="scope">
          <el-tag v-for="(e, i) in scope.row.clothingid" :key="i" v-if="clothing[e - 1]" style="margin: 0 4px 4px 0;">{{ clothing[e - 1].name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增裙子' : '信息修改'" :visible.sync="showAdd" width="500px">
      <el-form :model="skirt" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="裙子名称" prop="name">
          <el-input v-model="skirt.name"/>
        </el-form-item>
        <el-form-item label="商家名称" prop="memberid">
          <div class="select-box">
            <span v-if="nowMember.name">{{ nowMember.name }}</span>
            <el-button type="text" @click="toSelectMember">点击选择商家</el-button>
          </div>
          <!--<el-select v-model="skirt.memberid">-->
            <!--<el-option v-for="(e, i) in merList" :key="i" :value="e.id" :label="e.name"/>-->
          <!--</el-select>-->
        </el-form-item>
        <el-form-item label="团号" prop="uniteid">
          <el-select v-model="skirt.uniteid">
            <el-option value="1" label="1团"/>
            <el-option value="2" label="2团"/>
            <el-option value="3" label="3团"/>
            <el-option value="4" label="4团"/>
            <el-option value="5" label="5团"/>
          </el-select>
        </el-form-item>
        <el-form-item label="服装分类" prop="clothingid">
          <el-select v-model="skirt.clothingid" multiple style="width: 380px;">
            <el-option v-for="(e, i) in clothing" :key="i" :value="e.id" :label="e.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="风格分类" prop="styleid">
          <el-select v-model="skirt.styleid">
            <el-option v-for="(e, i) in style" :key="i" :value="e.id" :label="e.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌分类" prop="brandid">
          <el-select v-model="skirt.brandid">
            <el-option v-for="(e, i) in brand" :key="i" :value="e.id" :label="e.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="content">
          <el-input v-model="skirt.content" type="textarea"/>
        </el-form-item>
        <el-form-item label="裙子图片" prop="imgarr">
          <div class="image-box" v-for="(e, i) in skirt.imgarr" :key="i">
            <img :src="e" alt="" height="100" style="margin-right: 8px;">
            <i class="el-icon-close" @click="delImage(i)"></i>
          </div>
          <el-upload
            class="avatar-uploader"
            action="#"
            v-show="skirt.imgarr.length < 3"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
    <select-list ref="selectList" :url="'/Business/index?placardstatus=1&delflag=0'" :searchItems="memberSearch" :tableItem="memberTableItems" @selectRow="memberChange"/>
  </div>
</template>

<script>
import CLAZZ from '@/config/classify'
import {deleteRow, toExport} from '@/utils/getList'
import selectList from '@/components/common/selectList'
import tableItems from '@/config/tableItems'
export default {
  name: 'skirt',
  data () {
    return {
      searchItems: [
        {
          label: '裙子名称',
          type: 'input',
          key: 'name'
        },
        {
          label: '上传时间',
          type: 'times',
          key: 'times'
        },
        {
          label: '商家名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '是否过期',
          type: 'select',
          key: 'status',
          options: [{id: '1', name: '是'}, {id: '0', name: '否'}]
        },
        {
          label: '团号',
          type: 'input',
          key: 'uniteid'
        },
        {
          label: '服装类型',
          type: 'selects',
          key: 'clothingid',
          options: CLAZZ.clothing
        },
        {
          label: '风格',
          type: 'selects',
          key: 'styleid',
          options: CLAZZ.style
        }
      ], // 检索结构
      memberSearch: [
        {
          label: '商家名称',
          type: 'input',
          key: 'name'
        },
        {
          label: '手机号码',
          type: 'input',
          key: 'phone'
        },
        {
          label: '服装范围',
          type: 'selects',
          key: 'managementid',
          options: CLAZZ.managementid
        },
        {
          label: '等级',
          type: 'select',
          key: 'level',
          options: CLAZZ.level
        }
      ],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      skirts: [],
      skirt: {
        name: '',
        memberid: '',
        uniteid: '',
        clothingid: '',
        styleid: '',
        brandid: '',
        content: '',
        imgarr: []
      },
      rules: {
        name: [
          { required: true, message: '请输入裙子名称', trigger: 'blur' }
        ],
        uniteid: [
          { required: true, message: '请输入团号', trigger: 'change' }
        ],
        styleid: [
          { required: true, message: '请选择裙子风格', trigger: 'blur' }
        ],
        brandid: [
          { required: true, message: '请选择裙子品牌', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入裙子详情', trigger: 'blur' }
        ],
        clothingid: [
          { required: true, message: '请选择服装风格', trigger: 'change' }
        ],
        imgarr: [
          { required: true, message: '请选择裙子图片', trigger: 'change' }
        ],
        memberid: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ]
      },
      showAdd: false,
      clothing: CLAZZ.clothing,
      style: CLAZZ.style,
      brand: CLAZZ.brand,
      memberTableItems: tableItems.memberList,
      isAdd: true,
      updateRow: null,
      loading: false,
      lifting: null,
      nowMember: {}
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Skirti/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            res.data.data.list.forEach(item => {
              item.clothingid = item.clothingid ? item.clothingid.split(',') : ''
            })
            this.skirts = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.skirt.imgarr = []
      this.nowMember = {}
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.nowMember = {name: row.membername}
      this.$nextTick(() => {
        Object.keys(this.skirt).forEach(key => {
          this.skirt[key] = row[key]
        })
        this.skirt.imgarr = []
        if (row.img1) this.skirt.imgarr.push(row.img1)
        if (row.img2) this.skirt.imgarr.push(row.img2)
        if (row.img3) this.skirt.imgarr.push(row.img3)
        this.$refs['addForm'].clearValidate()
      })
    },
    toSubmit () {
      let url = '/Skirti/add'
      let params = {}
      Object.keys(this.skirt).forEach(key => {
        if (this.skirt[key] instanceof Array && key !== 'imgarr') {
          params[key] = this.skirt[key].join(',')
        } else {
          params[key] = this.skirt[key]
        }
      })
      if (!this.isAdd) {
        url = '/Skirti/edit'
        params.id = this.updateRow.id
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              if (res.data.status === 1) {
                this.showAdd = false
                this.$message.success(res.data.msg)
                this.getList()
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          return false
        }
      })
    },
    toDelete (id) {
      deleteRow({url: '/Skirti/delete', id}, (res) => {
        if (res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.getList()
        }
      })
    },
    delImage (index) {
      this.skirt.imgarr.splice(index, 1)
    },

    beforeUpload (file) {
      this.imageUrl = URL.createObjectURL(file)
      let _self = this
      let reader = new FileReader()
      reader.onload = function (ev) {
        _self.skirt.imgarr.push(ev.currentTarget.result)
      }
      reader.readAsDataURL(file)
      return false
    },

    toExport () {
      this.loading = true
      let searchData = this.$refs['search'].search()
      toExport('/Skirti/excelindex', searchData, '裙子', () => {
        this.loading = false
      })
    },

    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
        // lifting: 'createtime asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    memberChange (row) {
      this.nowMember = row
      this.skirt.memberid = row.id
    },
    toSelectMember () {
      this.$refs['selectList'].toShow()
    }
  },
  components: {
    selectList
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .avatar-uploader{
    display: inline-block;
    overflow: hidden;
  }
  .image-box{
    display: inline-block;
    position: relative;
  }
  .image-box i{
    position: absolute;
    top: 10px;
    right: 10px;
  }
</style>
